package com.yckj.day10;

/**
 * TODO
 *
 * @author shengyuee
 * @version 1.0
 * 2025/7/29 10:33
 */
public class StringDemo1 {
    public static void main(String[] args) {
        // 字符串对象创建方式
        String s1 = "hello";
        String s2 = new String("Hello");
        String s3 = new String("hello".getBytes());
        //  创建对象后，对象存储在堆空间，字符串对象存储在常量池中
        // 常用方法
        // 1. 获取字符串长度
        int length = s1.length();
        System.out.println("length = " + length);
        // 2. 获取字符
        char c = s1.charAt(1);
        System.out.println("c = " + c);
        // 3. 获取字符串中指定位置的索引值,获取指定内容第一次出现的索引值
        int index = s1.indexOf("l");
        System.out.println("index = " + index);
        // 4. 判断字符串是否相等, 区分大小写,比较的时内容
        boolean equals = s1.equals(s2);
        System.out.println("equals = " + equals);
        // 5. 判断字符串是否相等, 不区分大小写,比较的时内容
        boolean equalsIgnoreCase = s1.equalsIgnoreCase(s2);
        System.out.println("equalsIgnoreCase = " + equalsIgnoreCase);
        // 6. 判断字符串是否以指定内容开头
        boolean startsWith = s1.startsWith("he");
        System.out.println("startsWith = " + startsWith);
        // 7. 判断字符串是否以指定内容结尾
        boolean endsWith = s1.endsWith("lo");
        System.out.println("endsWith = " + endsWith);
        // 8. 判断字符串是否为空
        boolean empty = s1.isEmpty();
        System.out.println("empty = " + empty);
        if(s1.isEmpty()){
            System.out.println("字符串为空");
        }else {
            System.out.println("字符串不为空");
        }
        // 9. 获取字符串中指定内容出现的次数
        int count = s1.length() - s1.replace("l", "").length();
        System.out.println("count = " + count);
        // 10. 获取字符串中指定内容最后一次出现的索引值
        int index1 = s1.lastIndexOf("l");
        System.out.println("index1 = " + index1);
        // 11. 判断字符串是否包含字串
        boolean contains = s1.contains("ll");
        System.out.println("contains = " + contains);
        // 12. 获取字符串的字节长度
        int length1 = s1.getBytes().length;
        System.out.println("length1 = " + length1);
        // 13. 字符串截取,不包含后面的索引值
        String substring = s1.substring(1, 3);
        System.out.println("substring = " + substring);
        // 14.拆分字符串
        String str = "apple-banana-pear";
        String[] split = str.split("-");
        for (String s : split) {
            System.out.println("s = " + s);
        }
        // 14. 字符串替换
        String replace = s1.replace("l", "M");
        System.out.println("replace = " + replace);
        // 15. 字符串转大写
        String toUpperCase = s1.toUpperCase();
        System.out.println("toUpperCase = " + toUpperCase);
        // 16. 字符串转小写
        String toLowerCase = s1.toLowerCase();
        System.out.println("toLowerCase = " + toLowerCase);
        // 17. 删除字符串的空格
        String s11 = "  hello  ";
        String trim = s11.trim();
        System.out.println("trim = " + trim);
        // 18. 字符串连接
        String concat = s1.concat(" world");
        System.out.println("concat = " + concat);
        // 19.将字符串转换成字符数组
        char[] chars = s1.toCharArray();
        for (char c1 : chars) {
            System.out.println("c1 = " + c1);
        }
        // 20.将基本数据类型转换成String
        String s = String.valueOf(45);
        System.out.println("s = " + s);

        String ss1 = "welcome" + "hello";
         ss1 = "java";
    }
}
